package com.llp.declaresystem.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llp.declaresystem.domain.Result;
import com.llp.declaresystem.domain.dto.UserDto;
import com.llp.declaresystem.domain.entity.*;
import com.llp.declaresystem.mapper.ProjectMapper;
import com.llp.declaresystem.mapper.UnitMapper;
import com.llp.declaresystem.mapper.UserMapper;
import com.llp.declaresystem.service.ExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: declare-system
 * @BelongsPackage: com.llp.declaresystem.controller
 * @Author: bovan
 * @Date: 2022/11/24 10:16
 * @Description:
 */
@RestController
@RequestMapping(value = "/api/expert")
public class ExpertManagerController {
    @Autowired
    private ExpertService expertService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UnitMapper unitMapper;

    /**
     * 修改用户信息
     *
     * @param expert
     * @return
     */
    @PostMapping("/updateByName")
    public Result updateByName(@RequestBody Expert expert) {
        int res = expertService.updateByName(expert);
        if (res == 0) {
            return new Result<>(400, "修改失败", null);
        } else {
            return new Result<>(200, "修改成功", null);
        }
    }

    /**
     * 根据uid修改密码
     *
     * @param userDto
     * @return
     */
    @PostMapping("/updatePwd")
    public Result updatePwdByUid(@Validated @RequestBody UserDto userDto) {
        User user = userMapper.selectByUid(userDto.getUid());
        if(user!= null){
            if (user.getPassword().equals(userDto.getOldPassword())) {
                int res = expertService.updatePwdByUid(userDto);
                if (res == 1) {
                    return new Result<>(200, "修改成功", null);
                }
            }else{
                return new Result<>(400, "旧密码输入错误", null);
            }
        }
        return new Result<>(400, "修改失败", null);
    }

    @GetMapping("/select/categories/{uid}")
    public Result selectAllCategories(@PathVariable("uid") Integer uid) {
        List<ProjectCategory> projectCategory = expertService.selectAllProjectCategory(uid);
        if (projectCategory != null) {
            return new Result<>(200, "操作成功",
                    MapUtil.builder().put("categories", projectCategory).map());
        } else {
            return new Result<>(400, "操作失败", null);
        }
    }

    /**
     * 分页器 返回项目数据
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/select/allprojects/{currentPage}/{pageSize}")
    public Result selectAllProjects(@PathVariable int currentPage, @PathVariable int pageSize) {
        IPage<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        IPage<Map<String, Object>> mapIPage = projectMapper.selectMapsPage(page, null);
        //注意：此行必须使用 mapIPage 获取记录列表，否则会有数据类型转换错误
        mapIPage.getRecords().forEach(System.out::println);
        System.out.println("当前页号:" + page.getCurrent());
        System.out.println("获取页号:" + page.getPages());
        System.out.println("每页条数：" + page.getSize());
        System.out.println("总信息条数:" + page.getTotal());
        return new Result<>(200, "操作成功", page);
    }

    /**
     * 根据categoryid查询项目信息
     * @param cid
     * @return
     */
    @GetMapping("/select/projects/{cid}")
    public Result selectProjectByCid(@PathVariable("cid") Integer cid) {
        List<Project> projects = expertService.selectProjectByCid(cid);
        return new Result<>(200, "操作成功",
                MapUtil.builder()
                        .put("projectInfo", projects)
                        .map());
    }
    /**
     * 根据projectId查询项目信息
     *
     * @param pid
     * @return
     */
    @GetMapping("/select/projectInfo/{pid}")
    public Result selectProjectByPid(@PathVariable("pid") Integer pid) {
        Project project = expertService.selectProjectByPid(pid);
        return new Result<>(200, "操作成功",
                MapUtil.builder()
                        .put("projectInfo", project)
                        .map());
    }

    @PostMapping(value = "/submitProjectInfo")
    public Result submitProjectInfo(@RequestBody Project project){
        int res = expertService.updateProjectInfo(project);
        if(res != -1){
            return new Result<>(200, "提交成功", null);
        }else{
            return new Result<>(400, "提交失败", null);
        }
    }

}
